//
// Created by ZLY on 2025/5/9.
//

#include "collider.h"

Collider *Collider::addColliderChild(ObjectScreen *parent, glm::vec2 size, Type type,Anchor anchor) {
    auto collider = new Collider();
    collider->init();
    collider->setParent(parent);
    collider->setSize(size);
    collider->setType(type);
    parent->addChild(collider);
    return collider;
}

bool Collider::isColliding(Collider *other) {
    if (!other) {
        return false;
    }
    if (type_ == Type::CIRCLE && other->getType() == Type::CIRCLE) {
        auto point1 = parent_->getPosition() + offset_ + size_.x / 2.0f;
        auto point2 = other->getParent()->getPosition() + other->getOffset() + other->getSize().x / 2.0f;
        return glm::length(point1 - point2) < (size_.x + other->getSize().x) / 2.0f;
    }
    // TODO:其他碰撞
    return false;
}

void Collider::render() {
#ifdef DEBUG_MODE
    ObjectAffiliate::render();
    auto pos = parent_->getRenderPos()+offset_;
    game_.renderFillCircle(pos,size_,0.3);
#endif
}
